var util = require('../../utils/util.js');
//获取应用实例
const app = getApp();
Page({
    data: {
        pagenum: 1,
        lastnum:0,
      pageData: {
        to: 0,
        total: 1
      },
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        dataList:[]
    },
    // 下拉刷新 
    onPullDownRefresh: function () {
        wx.setNavigationBarTitle({
            title: '刷新中……'
        })
        var that = this;
      that.setData({
        isHideLoadMore: false,
        isLoadEnd: false,
        pagenum: 1,
        pageData: {
          to: 0,
          total: 1
        }
      })
        that.setData({
            pagenum: 1
        })
        that.getData();
        wx.setNavigationBarTitle({
            title: '抽奖助手'
        })
        wx.stopPullDownRefresh();
    },
    // 上拉加载
    onReachBottom: function () {
        var that = this;
      var toNum = that.data.pageData.to;
      var totalNum = that.data.pageData.total;
        var pagenum = that.data.pagenum;
      if (toNum != totalNum) {
        pagenum += 1;
        that.setData({
          pagenum: pagenum
        })
        //获取分页
        util.wGet('user/join', { page: that.data.pagenum }).then((res) => {
          var resData = res.data.data.data;
          for (var i = 0; i < resData.length; i++) {
            resData[i].activity.open_at = resData[i].activity.open_at.slice(0, -3);
          }
          if (resData == '') {
            that.setData({
              isHideLoadMore: true,
              isLoadEnd: true
            })
          }
          var toStr = 'pageData.to';
          var totalStr = 'pageData.total';
          that.setData({
            [toStr]: res.data.data.to,
            [totalStr]: res.data.data.total
          })
          for (var i = 0; i < resData.length; i++) {
            that.data.dataList.push(resData[i])
          }
          
          that.setData({
            dataList: that.data.dataList
          })
        })
      } else {
        that.setData({
          isHideLoadMore: true,
          isLoadEnd: true
        })
      }
    },
    onLoad: function() {
        // app.getUser();
        this.getData();
    },
    onShow:function(){
      // if (wx.pageScrollTo) { //回到顶部
      //   wx.pageScrollTo({
      //     scrollTop: 0
      //   })
      // }
      // this.setData({
      //   isLoadEnd: false,
      //   isHideLoadMore: false,
      //   pagenum: 1,
      //   pageData: {
      //     to: 0,
      //     total: 1
      //   }
      // });
      //   this.getData();
    },
    getData: function() {
        var that = this;
        that.setData({
            pagenum: 1
        })
      util.wGet('user/join', {
        page: that.data.pagenum
      }).then((res) => {
        if (res.data.data == '') {
        
        } else {
          var resData = res.data.data.data;
          for (var i = 0; i < resData.length; i++) {
            resData[i].activity.open_at = resData[i].activity.open_at.slice(0, -3);
          }
          that.setData({
            dataList: resData
          })
          if (that.data.dataList.length<4){
            that.setData({
              isHideLoadMore: true,
              isLoadEnd: true
            })
          }
          if (that.data.dataList == '') {
            // wx.showToast({
            //   icon: "none",
            //   title: "暂无记录"
            // })
            that.setData({
              isHideLoadMore: true,
              isLoadEnd: true
            })
          }
        }
      })
        // wx.request({
        //     url: 'https://wechat.wuyuzhong.com/api/user/join',
        //     data: {
        //         page: that.data.pagenum
        //     },
        //     header: {
        //         'Content-Type': 'application/json',
        //         'Accept': 'application/json',
        //         Authorization: num
        //     },
        //     success: function(res) {
        //         if (res.data.data == '') {
        //             wx.showToast({
        //                 icon: "none",
        //                 title: "暂无记录"
        //             })
        //         } else {
        //             that.setData({
        //                 dataList: res.data.data.data
        //             })
        //         }
        //     }
        // })
    },
    showActivity: function(e) {
        var key = e.currentTarget.dataset.key;
        wx.navigateTo({
            url: '../activity/activity?key=' + key
        })
    }
})